package com.kmzx.one.project.controller;

import com.kmzx.one.common.exceptions.NoSuchObjectException;
import com.kmzx.one.common.exceptions.ParameterNullException;
import com.kmzx.one.project.net.BaseResult;
import com.kmzx.one.project.net.UserResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping(value = "kmzx")
public class BaseController {

    protected final Logger logger = LoggerFactory.getLogger(getClass());


    protected int pageNum = 1;
    protected int pageSize = 10;

    @Autowired
    private RedisTemplate<String, Object> objectRedisTemplate;


    @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
    }


    public BaseResult handleException(Exception e) {
        if (e instanceof ParameterNullException) {
            BaseResult error = BaseResult.Builder.FAIL();
            error.setErrCode(402);
            error.setErrMsg(e.getMessage());
            error.setCode(402);
            error.setMsg(e.getMessage());
            return error;
        } else if (e instanceof NoSuchObjectException) {
            BaseResult error = BaseResult.Builder.FAIL();
            NoSuchObjectException noE = (NoSuchObjectException) e;
            Integer code = noE.getCode();
            if (code != null && code != 0) {
                error.setErrCode(code);
                error.setCode(code);
            } else {
                error.setCode(401);
                error.setErrCode(401);
            }
            error.setErrMsg(e.getMessage());
            error.setMsg(e.getMessage());
            return error;
        }
        BaseResult fail = BaseResult.Builder.FAIL().setData(null);
        e.printStackTrace();
        logger.error("消息:{},{}", e.getMessage(), e.getCause());
        return fail;
    }

    public UserResult handleUserException(Exception e) {
        if (e instanceof ParameterNullException) {
            UserResult error = UserResult.Builder.FAIL();
            error.setErrCode(402);
            error.setErrMsg(e.getMessage());
            return error;
        } else if (e instanceof NoSuchObjectException) {
            UserResult error = UserResult.Builder.FAIL();
            error.setErrCode(401);
            error.setErrMsg(e.getMessage());
            return error;
        }
        UserResult fail = UserResult.Builder.FAILServer().setData(null);
        e.printStackTrace();
        logger.error("消息:{},{}", e.getMessage(), e.getCause());
        return fail;
    }

    public void beforeAopRepeat(String params) {
        // 将入参作为key自增值存入redis
        ValueOperations<String, Object> value = objectRedisTemplate.opsForValue();
        Long result = value.increment(params, 1);
        // 判断返回值是否大于1，如果是则重复提交
        if (result > 1) {
            throw new NoSuchObjectException(403, "参数重复提交");
        }
        objectRedisTemplate.expire(params, 5, TimeUnit.SECONDS); // 设置过期时间5秒
    }
}
